<?php

namespace App\Http\Controllers\Home;

use App\Model\Earn;
use App\Model\Goods;
use App\Model\Grid;
use Illuminate\Http\Request;
use DB;
use Auth;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function index(){
        $data['grids'] = Grid::where('status',1)->orderby('sindex','desc')->get();
        $user = Auth::guard('home')->user();
        if(empty($user)){
            $data['status'] = 1;
            $data['new'] = Goods::where('type1',1)->where('type2',0)->where('status',1)->skip(0)->take(2)->get();
        }elseif ((time()-strtotime($user->created_at))>=0 && (time()-strtotime($user->created_at))<30*24*60*60 ){
            $data['status'] = 2;
            $data['new'] = Goods::where('type1',1)->where('type2',0)->where('status',1)->skip(0)->take(5)->get();
        }else{
            $data['status'] = 3;
            $data['new'] = [];
        }
        $data['notnew'] = Goods::where('type1',0)->where('type2',0)->where('status',1)->skip(0)->take(5)->get();

        return view('home.index',['data'=>$data]);
    }

    public function more(Request $request){
        $return = array();
        do{
            $page = $request->input('page',1);
            $pagesize = $request->input('pagesize',10);
            $data = Goods::where('type1',0)->where('type2',0)->where('status',1)->skip(5+($page-1)*$pagesize)->take($pagesize)->get();
            foreach ($data as $k => $v){
                $data[$k]->price = sprintf('%.2f',$v->price/100);
            }
            $return['code'] = 0;
            $return['message'] = '请求成功';
            $return['data'] = $data;

        }while(false);

        return json_encode($return);
    }


    public function alllist(){
        $data = Earn::select(DB::raw("sum(money) as total,uid"))->groupby('uid')->orderby('total','desc')->skip(0)->take(25)->get();
        return view('home.money_list',['data'=>$data]);
    }

    public function content($id){
        $user = Auth::guard('home')->user();
        if(!empty($user) && $user->status == 0){
            return redirect('/personal');
        }
        $data = Goods::where('id',$id)->where('status',1)->first();
        if(!empty($user) && $user->status==1){
            $data->surplus = $data->surplus - 1;  //剩余量
            if($data->surplus >= 0){
                $data->save();

                $info = new Earn();
                $info->uid = $user->id;
                $info->gid = $id;
                $info->money = $data->price;
                $info->save();
            }
        }
        return view('home.content',['data'=>$data]);
    }

    public function slide(){
        $data = Goods::where('type1',0)->where('type2',1)->where('status',1)->get();
        return view('home.slide',['data'=>$data]);
    }

    public function earn(Request $request){
        $return = array();
        do{
            $id = $request->input('id',0);
            $goods = Goods::where('id',$id)->where('status',1)->first();
            if(empty($goods)){
                $return['code'] = 101;
                $return['message'] = '文章不存在';
                break;
            }
            $user = Auth::guard('home')->user();
            if(empty($user)){
                $return['code'] = 102;
                $return['message'] = '请登录';
                break;
            }
            if($user->status == 0){
                $return['code'] = 103;
                $return['message'] = '请先进行激活';
                break;
            }
            //每个用户 一天最高赚80元  超过了观看广告也不弹出乐  也不赚钱了   从00：00开始又开始了
            $begin = date('Y-m-d H:i:s',strtotime(date('Y-m-d')));
            $end = date('Y-m-d H:i:s',strtotime(date('Y-m-d'))+24*60*60);
            $today = Earn::where('uid',$user->id)->where('created_at','<',$end)->where('created_at','>',$begin)->sum('money');
            if($today/100 >= 80){
                $return['code'] = 104;
                $return['message'] = '今日收益已达上限';
                break;
            }

            $goods->surplus = $goods->surplus - 1;  //剩余量
            if($goods->surplus >= 0){
                $goods->save();

                $info = new Earn();
                $info->uid = $user->id;
                $info->gid = $id;
                $info->money = $goods->price;
                $info->save();

                $return['code'] = 0;
                $return['message'] = '挣钱成功';
                $return['data'] = sprintf('%.2f',$goods->price/100);
                break;
            }


        }while(false);

        return json_encode($return);
    }
}
